//
//  FileRouter.swift
//  soapp_Example
//
//  Created by zhuxietong on 2019/4/27.
//  Copyright © 2019 CocoaPods. All rights reserved.
//

import Foundation
import soapp
public enum FileRouter:AppRouter {
    case video
    case audio
    case image
    case unkown
    
    public static var image_dots:[String] = ["png","jpg","jpeg"]
    public static var audio_dots:[String] = ["mp3"]
    public static var video_dots:[String] = ["png","jpg","jpeg"]

    
    public static func dot(url:String)->String {
        var file_extention = ""
        let reg = Regex(".+\\.([\\w|\\d]+)$")
        if let subject = reg.firstMatch(in: url)?.captures[0] {
            file_extention = subject
        }
        return file_extention.lowercased()
    }
    
    public static func type(url:String)->FileRouter?{
        let _dot = FileRouter.dot(url: url)
        if image_dots.contains(_dot){
            return .image
        }
        if video_dots.contains(_dot){
            return .video
        }
        if audio_dots.contains(_dot){
            return .audio
        }
        
        return nil
    }

    
    public static func open(link: String, at: UIViewController?, present: NavPresentType?) -> Bool {
        
        guard let router = FileRouter.type(url: link) else {
            return false
        }
        
        if link.hasPrefix("file://")
        {
            
            switch router{
            case .audio:
                let ctr = EeVideoPlayer()
                ctr.mo.model["url"] = link
                return true
            case .image:
                break
            case .video:
                break
            case .unkown:
                break
            }
        }
        
        return false
    }
}
